<template>
  <div>
    <headNav :title="title" ></headNav>
    <!-- 编辑头像模块 -->
    <div class="flexHeight headImgCss" v-if="title == '编辑头像'" >
      <div class="flexCenter">
        <img v-if="!identityData.portraitImgUrl && imgDataSource.length == 0" src="@/assets/img/default_heads.png" mode="" @click="uploadImg" >
        <img v-if="identityData.portraitImgUrl && imgDataSource.length == 0" :src="identityData.portraitImgUrl" mode="" @click="uploadImg" >
        <img v-if="imgDataSource.length > 0" :src="imgDataSource[imgDataSource.length - 1]" alt=""  @click="uploadImg" >
      </div>
       <media ref="media" :showVoice='false' :showVideo='false' :showLink='false' style="display:none"></media>
      <span>点击图片更换头像</span>
      <button type="default" @click="imgClick" >确定</button>
    </div>

    <!-- 编辑称呼 -->
    <div class="flexHeight headNameCss" v-if="title == '编辑称呼'" >
      <div class="flexAlign" >
        <span>称呼：</span>
        <input type="text" v-model="identityData.name" placeholder="请输入名称" />
      </div>
      <button type="default" @click="editNameClick" >确定</button>
    </div>

    <!-- 修改密码 -->
    <div v-if="title == '修改密码'" >
      <div class="flexAlign inputDivCss" >
        <input type="password" v-model="pwdData.oldpwd" placeholder="请输入原始密码" />
      </div>
      <div class="flexAlign inputDivCss" >
        <input type="password" v-model="pwdData.newpwd" placeholder="请输入新密码" />
      </div>
      <div class="flexAlign inputDivCss" >
        <input type="password" v-model="pwdData.newpwd2" placeholder="确认新密码" />
      </div>
      <div class="flexCenter" style="width: 100%; color: rgb(136, 133, 133); margin-top: 0.5rem;" >
        <span>设置密码要求英文+数字组合，长度为6-20</span>
      </div>

      <div class="flexCenter" style="width: 100%;" >
        <button type="default" @click="pwdClick" >确定</button>
      </div>
    </div>

  </div>
</template>

<script>
import headNav from '@/components/headNav/headNav.vue'
import { mapActions, mapState, mapGetters, mapMutations } from 'vuex'
import { putChangeNicknameApi, putChangeHeadImgApi, putChangepwd } from '@/api/myNewApi'
import media from '@/components/media'
export default {
  components: {
    headNav,
    media
  },
  computed: {
    ...mapState({
      studentId: state => state.user.studentId,
      loginUserId: state => state.user.loginUserId,
      roleCode: state => state.user.roleCode,
      imgDataSource () {
        return this.$store.state.media.imgDataSource
      },
    })
  },
  data () {
    return {
      title: '',   // 需要修改什么方式
      identityData: '',  // 登录用户信息
      headImg: '',  // 上传选择的头像
      pwdData: {
        oldpwd: '',
        newpwd: '',
        newpwd2: ''
      }
    }
  },
  methods: {
    // 修改密码确定按钮
    pwdClick () {
      for(var key in this.pwdData) {
        if (!this.pwdData[key]){
          this.$vux.toast.text('请完整填写修改信息', 'middle')
          return;
        }
      }

      var regNumber = /\d+/
      var regString = /[a-zA-Z]+/
      if (regNumber.test(this.pwdData.newpwd) && regString.test(this.pwdData.newpwd)) {
          console.log('str1：验证成功');
      }else{
          this.$vux.toast.text('设置密码要求英文+数字组合，长度为6-20', 'middle')
          return
      }

      if (6 > this.pwdData.newpwd.length  || this.pwdData.newpwd.length > 20) {
          this.$vux.toast.text('设置密码要求英文+数字组合，长度为6-20', 'middle')
          return
      }
      // var pwdRegex = new RegExp("(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,20}");
      // if (!pwdRegex.test(this.pwdData.newpwd)) {
      //   this.$vux.toast.text('设置密码要求英文+数字+字符组合，长度为6-20', 'middle')
      //   return;
      // }

      if (this.pwdData.newpwd != this.pwdData.newpwd2) {
        this.$vux.toast.text('新密码两次输入不一致', 'middle')
        return;
      }
      this.$vux.confirm.show({
          title: '提示',
          content: '是否确定修改密码？',
          onConfirm : async () => {
            this.packChangePwd()
          },
          onCancel () {
          }
      })
    },
    // 上传图片按钮触发事件
    async uploadImg(){
      this.$refs.media.chooseImage(1)
    },
    // 修改头像确定按钮触发事件
    async imgClick () {
      if (this.imgDataSource.length == 0) {
        this.$vux.toast.text('请选择需要更换的头像', 'middle')
        return
      }
      this.$vux.confirm.show({
          title: '提示',
          content: '是否确定修改头像？',
          onConfirm : async () => {
            this.packChangeImg()
          },
          onCancel () {
          }
      })
    },
    // 修改昵称确定按钮触发事件
    async editNameClick () {
      this.identityData.name = this.identityData.name.replace(/ /g,'')
      if (!this.identityData.name) {
        this.$vux.toast.text('昵称不能为空', 'middle')
        return
      }

      if (this.identityData.name.length > 8) {
        this.$vux.toast.text('设置昵称过长，限制8个字', 'middle')
        return
      }

      this.$vux.confirm.show({
          title: '提示',
          content: '是否确定修改？',
          onConfirm : () => {
            this.packChangeName()
            this.$vux.toast.text('修改成功', 'middle')
            this.$router.push('myNew')
          },
          onCancel () {
          }
      })
    },
    // 修改密码封装
    async packChangePwd () {
      let params = {
        id: this.loginUserId,
        data: {
          oldPwd: this.pwdData.oldpwd,
          newPwd: this.pwdData.newpwd,
          confirmPwd: this.pwdData.newpwd2
        }
      }
      let res = await putChangepwd(params)
      this.$vux.toast.text('修改成功! 请重新登录', 'middle')
      this.$store.dispatch('setting/loginOut')
      console.log(res)
    },
    // 修改昵称封装  type: 老师（teacherName）  家长（parentName）
    async packChangeName () {
      let type = ''
      this.roleCode == 'Teacher' ? type = 'teacherName' : type = 'parentName'
      let params = {
        id: this.loginUserId,
        type: type,
        data: {
          name: this.identityData.name
        }
      }
      await putChangeNicknameApi(params)
      this.$vux.toast.text('修改成功', 'middle')
    },
    // 修改头像封装  type: 老师（teacherHeadImg）  家长（parentHeadImg）
    async packChangeImg () {
      await this.$store.dispatch('media/upload')
      this.headImg =  this.$store.state.media.imgData[this.$store.state.media.imgData.length - 1]
      let type = ''
      this.roleCode == 'Teacher' ? type = 'teacherHeadImg' : type = 'parentHeadImg'
      let params = {
        id: this.loginUserId,
        type: type,
        data: {
          imgUrl: this.headImg
        }
      }
      await putChangeHeadImgApi(params)
      this.$vux.toast.text('修改成功', 'middle')
      this.$router.push('myNew')
    }
  },
  mounted() {
    this.title = localStorage.editClass
    this.identityData = JSON.parse(localStorage.identityData)
    console.log(localStorage.editClass)
  }
}
</script>

<style lang="less" scoped >
.inputDivCss {
  width: 6.2rem;
  height: 1rem;
  margin: 0.3rem 0 0 0.5rem;
  border-bottom: 1px solid #bdbdbd;
}

.headNameCss {
  .flexHeight;
  justify-content: center;
  align-items: center;
  div {
    width: 6.2rem;
    height: 1.2rem;
    margin-left: 0.2rem;
    border-bottom: 1px solid #bdbdbd;
    font-size: 0.3rem;
  }
}

.headImgCss {
  justify-content: center;
  align-items: center;
  font-size: 0.32rem;
  color: #999999;
  div {
    width: 100%;
    height: 3rem;
    margin-top: 0.5rem;
  }
  img {
    width: 2rem;
    height: 2rem;
    border-radius: 2.5rem;
  }
}

button {
  width: 5rem;
  height: 0.7rem;
  color: #FFF;
  background: #128fef;
  border-radius: 0.3rem;
  border: 0;
  font-size: 0.3rem;
  margin-top: 0.5rem;
}

input {
  width: 4.5rem;
  height: 0.6rem;
  margin-left: 0.2rem;
  padding-left: 0.2rem;
  border-radius: 0.2rem;
  font-size: 0.3rem;
  background: #f4f4f4;
}

.flexCenter {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flexHeight {
  display: flex;
  flex-direction: column;
}

.flexAlign {
  display: flex;
  align-items: center;
}

.flexEdge {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
